package com.victor.controller.web;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.victor.entity.city.CityIntroduce;
import com.victor.entity.city.CityIntroducePhoto;
import com.victor.entity.comments.Comments;
import com.victor.service.CityService;
import com.victor.utils.ClassFilter;
import com.victor.utils.GetPage;
import com.victor.utils.Msg;

/**
 * 后台城市管理
 * 
 * @author qinyuquan
 *
 */
@RestController
@RequestMapping("/city")
public class CityController {

	@Autowired
	private CityService service;
	@Autowired
	private LogController log;
	
	/**
	 * 城市管理页
	 * @param search
	 * @param page
	 * @param row
	 * @param province
	 * @param type
	 * @param adminid
	 * @return
	 */
	@RequestMapping("/all")
	public Msg findAll(String search,int page,int row,String province) {
		PageInfo<CityIntroduce> all = service.selAll(search,page,row,province);
		return Msg.success().add("list", all);
	}
	/**
	 * 新建城市
	 * @param cityIntroduce
	 * @return
	 */
	@RequestMapping("/addcity")
	public Msg addCity(@RequestBody CityIntroduce cityIntroduce) {
		String[] str = {"c_name","c_coreintroduce","photo","type","adminid"};
		boolean empty = ClassFilter.somePropertyIsEmpty(cityIntroduce, str);
		if (empty == true) {
			return Msg.fail("入参错误");
		}
		boolean insertCity = service.insertCity(cityIntroduce);
		if (!insertCity) {
			return Msg.fail("添加城市失败");
		}
		if (cityIntroduce.getPhotoslist() != null || cityIntroduce.getPhotoslist().size()>0) {
			for (int i = 0; i < cityIntroduce.getPhotoslist().size(); i++) {
				String valueOf = String.valueOf(cityIntroduce.getC_id());
				cityIntroduce.getPhotoslist().get(i).setCid(String.valueOf(cityIntroduce.getC_id()));
				boolean insertPhoto = service.insertPhoto(cityIntroduce.getPhotoslist().get(i));
				if (!insertPhoto) {
					return Msg.fail("添加图片失败");
				}
			}
		}
		log.addLog(cityIntroduce.getAdminid(), "添加了城市", cityIntroduce.getType());
		return Msg.success();
	}
	/**
	 * 城市详情
	 * @param id
	 * @param page
	 * @param row
	 * @param flag
	 * @return
	 */
	@RequestMapping("/details")
	public Msg cityDetails(String id) {
		if (id == null || id.isEmpty()) {
			return Msg.fail("入参错误");
		}
		String uid = null;
		CityIntroduce city = service.selCityById(id,uid);
		return Msg.success().add("city", city);
		
	}
	/**
	 * 修改城市
	 * @param city
	 * @return
	 */
	@RequestMapping("/updatecity")
	public Msg updateCity(@RequestBody CityIntroduce city) {
		String[] str = {"c_id","c_name","c_coreintroduce","c_voiceintroduce","c_voiceintroduce_english","c_introduce","video","photo","qjphoto","type","adminid"};
		boolean empty = ClassFilter.somePropertyIsEmpty(city, str);
		if (empty == true) {
			return Msg.fail("入参错误");
		}
		boolean updateCity = service.updateCity(city);
		if (city.getPhotoslist() != null || city.getPhotoslist().size()>0) {
			service.delPhoto(city.getC_id());
			for (int j = 0; j < city.getPhotoslist().size(); j++) {
				city.getPhotoslist().get(j).setCid(String.valueOf(city.getC_id()));
				boolean b = service.insertPhoto(city.getPhotoslist().get(j));
				if (!b) {
					return Msg.fail("添加图片失败");
				}
			}
		}
		log.addLog(city.getAdminid(), "修改了城市", city.getType());
		return Msg.judge(updateCity);
	}
	/**
	 * 删除/批量删除 城市
	 * @param ids
	 * @return
	 */
	@RequestMapping("/moredelete")
	public Msg deltateCity(int[] ids,String adminid,String type) {
		if (ids == null || ids.length<=0 || adminid == null || adminid.isEmpty() || type == null || adminid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		boolean b = service.delCity(ids);
		if (!b) {
			return Msg.fail("删除城市失败");
		}
		log.addLog(adminid, "删除了城市", type);
		return Msg.success();
	}
	
	/**
	 * 删除评论
	 * @param id
	 * @return
	 */
	@RequestMapping("/delcomments")
	public Msg delComments(String id,String adminid,String type) {
		if (id == null || id.isEmpty()) {
			return Msg.fail("入参错误");
		}
		boolean b = service.delComments(id);
		log.addLog(adminid, "删除了城市评论", type);
		return Msg.judge(b);
	}
	
	/**
	 * 新建图片 修改图片  删除图片
	 * @param list
	 * @return
	 */
	@RequestMapping("/updatephoto")
	public Msg photo(@RequestBody List<CityIntroducePhoto> list) {
		if (list == null || list.size()<=0) {
			return Msg.fail("入参失败");
		}
		//删除全部图片
		service.delPhoto(Integer.valueOf(list.get(0).getCid()));
		for (int i = 0; i < list.size(); i++) {
			String[] str = {"cp_photopath","cid"};
			boolean empty = ClassFilter.somePropertyIsEmpty(list.get(i), str);
			if (empty == true) {
				return Msg.fail("图片参数错误");
			}
			//添加图片
			boolean b = service.insertPhoto(list.get(i));
			if (!b) {
				return Msg.fail("添加图片错误");
			}
			
		}		return Msg.success();
	}
	
}
